home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / io / OutputStreamWriter.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  7.4 KB  |  260 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)OutputStreamWriter.java    1.22 98/08/07
  3.  *
  4.  * Copyright 1996-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.io;
  16.  
  17. import sun.io.CharToByteConverter;
  18. import sun.io.ConversionBufferFullException;
  19.  
  20.  
  21. /**
  22.  * Write characters to an output stream, translating characters into bytes
  23.  * according to a specified character encoding.  Each OutputStreamWriter
  24.  * incorporates its own CharToByteConverter, and is thus a bridge from
  25.  * character streams to byte streams.
  26.  *
  27.  * <p> The encoding used by an OutputStreamWriter may be specified by name, by
  28.  * providing a CharToByteConverter, or by accepting the default encoding, which
  29.  * is defined by the system property <tt>file.encoding</tt>.
  30.  *
  31.  * <p> Each invocation of a write() method causes the encoding converter to be
  32.  * invoked on the given character(s).  The resulting bytes are accumulated in a
  33.  * buffer before being written to the underlying output stream.  The size of
  34.  * this buffer may be specified, but by default it is large enough for most
  35.  * purposes.  Note that the characters passed to the write() methods are not
  36.  * buffered.  For top efficiency, consider wrapping an OutputStreamWriter
  37.  * within a BufferedWriter so as to avoid frequent converter invocations.  For
  38.  * example,
  39.  *
  40.  * <pre>
  41.  * Writer out
  42.  *   = new BufferedWriter(new OutputStreamWriter(System.out));
  43.  * </pre>
  44.  *
  45.  * @see BufferedWriter
  46.  * @see OutputStream
  47.  *
  48.  * @version     1.10, 97/01/27
  49.  * @author    Mark Reinhold
  50.  * @since    JDK1.1
  51.  */
  52.  
  53. public class OutputStreamWriter extends Writer {
  54.  
  55.     private CharToByteConverter ctb;
  56.     private OutputStream out;
  57.  
  58.     private static final int defaultByteBufferSize = 8192;
  59.     /* bb is a temporary output buffer into which bytes are written. */
  60.     private byte bb[];
  61.     /* nextByte is where the next byte will be written into bb */
  62.     private int nextByte = 0;
  63.     /* nBytes is the buffer size = defaultByteBufferSize in this class */
  64.     private int nBytes = 0;
  65.  
  66.     /**
  67.      * Create an OutputStreamWriter that uses the named character encoding.
  68.      *
  69.      * @param  out  An OutputStream
  70.      * @param  enc  Name of the encoding to be used
  71.      *
  72.      * @exception  UnsupportedEncodingException
  73.      *             If the named encoding is not supported
  74.      */
  75.     public OutputStreamWriter(OutputStream out, String enc)
  76.     throws UnsupportedEncodingException
  77.     {
  78.     this(out, CharToByteConverter.getConverter(enc));
  79.     }
  80.  
  81.     /**
  82.      * Create an OutputStreamWriter that uses the default character encoding.
  83.      *
  84.      * @param  out  An OutputStream
  85.      */
  86.     public OutputStreamWriter(OutputStream out) {
  87.     this(out, CharToByteConverter.getDefault());
  88.     }
  89.  
  90.     /**
  91.      * Create an OutputStreamWriter that uses the specified character-to-byte
  92.      * converter.  The converter is assumed to have been reset.
  93.      *
  94.      * @param  out  An OutputStream
  95.      * @param  ctb  A CharToByteConverter
  96.      */
  97.     private OutputStreamWriter(OutputStream out, CharToByteConverter ctb) {
  98.     super(out);
  99.     if (out == null) 
  100.         throw new NullPointerException("out is null");
  101.     this.out = out;
  102.     this.ctb = ctb;
  103.     bb = new byte[defaultByteBufferSize];
  104.     nBytes = defaultByteBufferSize;
  105.     }
  106.  
  107.     /**
  108.      * Returns the canonical name of the character encoding being used by 
  109.      * this stream.  If this <code>OutputStreamWriter</code> was created 
  110.      * with the {@link #OutputStreamWriter(OutputStream, String)} constructor, 
  111.      * the returned encoding name, being canonical, may differ from the 
  112.      * encoding name passed to the constructor.  May return <code>null</code> 
  113.      * if the stream has been closed.
  114.      */
  115.     public String getEncoding() {
  116.     synchronized (lock) {
  117.         if (ctb != null)
  118.         return ctb.getCharacterEncoding();
  119.         else
  120.         return null;
  121.     }
  122.     }
  123.  
  124.     /** Check to make sure that the stream has not been closed */
  125.     private void ensureOpen() throws IOException {
  126.     if (out == null)
  127.         throw new IOException("Stream closed");
  128.     }
  129.  
  130.     /**
  131.      * Write a single character.
  132.      *
  133.      * @exception  IOException  If an I/O error occurs
  134.      */
  135.     public void write(int c) throws IOException {
  136.     char cbuf[] = new char[1];
  137.     cbuf[0] = (char) c;
  138.     write(cbuf, 0, 1);
  139.     }
  140.  
  141.     /**
  142.      * Write a portion of an array of characters.
  143.      *
  144.      * @param  cbuf  Buffer of characters
  145.      * @param  off   Offset from which to start writing characters
  146.      * @param  len   Number of characters to write
  147.      *
  148.      * @exception  IOException  If an I/O error occurs
  149.      */
  150.     public void write(char cbuf[], int off, int len) throws IOException {
  151.     synchronized (lock) {
  152.         ensureOpen();
  153.             if ((off < 0) || (off > cbuf.length) || (len < 0) ||
  154.                 ((off + len) > cbuf.length) || ((off + len) < 0)) {
  155.                 throw new IndexOutOfBoundsException();
  156.             } else if (len == 0) {
  157.                 return;
  158.             }
  159.         int ci = off, end = off + len;
  160.         boolean bufferFlushed = false; 
  161.         while (ci < end) {
  162.         boolean bufferFull = false;
  163.         try {
  164.             nextByte += ctb.convertAny(cbuf, ci, end,
  165.                         bb, nextByte, nBytes);
  166.             ci = end;
  167.         }
  168.         catch (ConversionBufferFullException x) {
  169.             int nci = ctb.nextCharIndex();
  170.             if ((nci == ci) && bufferFlushed) {
  171.             /* If the buffer has been flushed and it 
  172.                still does not hold even one character */
  173.             throw new 
  174.                 CharConversionException("Output buffer too small");
  175.             }
  176.             ci = nci;
  177.             bufferFull = true;
  178.             nextByte = ctb.nextByteIndex();
  179.         } 
  180.         if ((nextByte >= nBytes) || bufferFull) {
  181.             out.write(bb, 0, nextByte);
  182.             nextByte = 0;
  183.             bufferFlushed = true;
  184.         }
  185.         }
  186.     }
  187.     }
  188.  
  189.     /**
  190.      * Write a portion of a string.
  191.      *
  192.      * @param  str  A String
  193.      * @param  off  Offset from which to start writing characters
  194.      * @param  len  Number of characters to write
  195.      *
  196.      * @exception  IOException  If an I/O error occurs
  197.      */
  198.     public void write(String str, int off, int len) throws IOException {
  199.     char cbuf[] = new char[len];
  200.     str.getChars(off, off + len, cbuf, 0);
  201.     write(cbuf, 0, len);
  202.     }
  203.  
  204.     /**
  205.      * Flush the output buffer to the underlying byte stream, without flushing
  206.      * the byte stream itself.  This method is non-private only so that it may
  207.      * be invoked by PrintStream.
  208.      */
  209.     void flushBuffer() throws IOException {
  210.     synchronized (lock) {
  211.         ensureOpen();
  212.  
  213.         for (;;) {
  214.         try {
  215.             nextByte += ctb.flushAny(bb, nextByte, nBytes);
  216.         }
  217.         catch (ConversionBufferFullException x) {
  218.             nextByte = ctb.nextByteIndex();
  219.         }
  220.         if (nextByte == 0)
  221.             break;
  222.         if (nextByte > 0) {
  223.             out.write(bb, 0, nextByte);
  224.             nextByte = 0;
  225.         }
  226.         }
  227.     }
  228.     }
  229.  
  230.     /**
  231.      * Flush the stream.
  232.      *
  233.      * @exception  IOException  If an I/O error occurs
  234.      */
  235.     public void flush() throws IOException {
  236.     synchronized (lock) {
  237.         flushBuffer();
  238.         out.flush();
  239.     }
  240.     }
  241.  
  242.     /**
  243.      * Close the stream.
  244.      *
  245.      * @exception  IOException  If an I/O error occurs
  246.      */
  247.     public void close() throws IOException {
  248.     synchronized (lock) {
  249.         if (out == null)
  250.         return;
  251.         flush();
  252.         out.close();
  253.         out = null;
  254.         bb = null;
  255.         ctb = null;
  256.     }
  257.     }
  258.  
  259. }
  260.